<?PHP

/**
 * LoginView 
 * 
 * @package 
 * @version $id$
 * @author Thomas Dahlberg 
 * @license PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
 */
class LoginView extends PageView {

    // TODO change to constants
    private $formUsernameName = "username";
    private $formPasswordName = "password" ;
    private $formLoginName = "login" ;
    private $formLogoutName = "logout" ;
    private $formRememberMeName = "rememberMe" ;


    /**
     * DoLoginBox 
     * 
     * @access public
     * @return string
     */
    public function doLoginBox() {

        return "
            <form>
                <label for=\"$this->formUsernameName\">Användarnamn:</label><br /> <input type=\"text\" name=\"$this->formUsernameName\" id=\"$this->formUsernameName\" /><br />
                <label for=\"$this->formPasswordName\">Lösenord:</label><br /> <input type=\"password\" name=\"$this->formPasswordName\" id=\"$this->formPasswordName\" /><br />
                <label for=\"$this->formRememberMeName\">Kom ihåg mig:</label> <input type=\"checkbox\" name=\"$this->formRememberMeName\" id=\"$this->formRememberMeName\"/>
                <input type=\"submit\" name=\"$this->formLoginName\" value=\"Logga in\" class=\"right\" /> 
            </form>
        " ;
    }
    /**
     * DoLogoutBox 
     * 
     * @access public
     * @return string
     */
    public function doLogoutBox() {
        return "
            <form>
                <input type=\"submit\" name=\"$this->formLogoutName\" value=\"Logga ut\" />
            </form>
        " ;
    }
    /**
     * GetUserName 
     * 
     * @access public
     * @return string
     */
    public function getUserName() {

        if($this->getCookie()) {
            return $this->getCookie() ;
        } else if ($_GET[$this->formUsernameName]) {
            return $_GET[$this->formUsernameName] ;
        } else {
            return NULL ;
        }
    }
    /**
     * GetPassword 
     * 
     * @access public
     * @return string
     */
    public function getPassword() {
        if($this->getCookie("password")) {
            return $this->getCookie("password") ;
        } else if (md5($_GET[$this->formPasswordName])) {
            return md5($_GET[$this->formPasswordName]) ;
        } else {
            return NULL ;
        }

        return (isset($_GET[$this->formPasswordName])) ? md5($_GET[$this->formPasswordName]) : NULL ;
    }
    /**
     * TriedToLogin 
     * 
     * @access public
     * @return bool
     */
    public function triedToLogin() {
    
        if(isset($_GET[$this->formLoginName]) || $this->isCookieSet()) {
            return true ;
        } else {
            return false ;
        }
    }
    /**
     * TriedToLogout 
     * 
     * @access public])
     * @return bool
     */
    public function triedToLogout() {
        return (isset($_GET[$this->formLogoutName])) ? true : false ;
    }
    /**
     * IsRememberMe 
     * 
     * @access public
     * @return void
     */
    public function isRememberMe() {
        return (isset($_GET[$this->formRememberMeName])) ? true : false ;
    }
    /**
     * GetCookie 
     * 
     * @param string $name 
     * @access public
     * @return string
     */
    public function getCookie($name = "username") {
        return (isset($_COOKIE[$name])) ? $_COOKIE[$name] : false ;
    }
    /**
     * IsCookieSet 
     * 
     * @param string $name 
     * @access public
     * @return bool
     */
    public function isCookieSet($name = "username") {
        return (isset($_COOKIE[$name])) ? true : false ;
    }
    /**
     * DoSetCookie 
     * 
     * @param string $value 
     * @param string $name 
     * @access public
     * @return bool
     */
    public function doSetCookie($value, $name = "username") {
        $_COOKIE[$name] = $value ;
        return setcookie($name, $value, time()+60*60*24*30) ;
    }
    /**
     * DoUnsetCookie 
     * 
     * @param string $name 
     * @access public
     * @return bool
     */
    public function doUnsetCookie($name = "username") {
        unset ($_COOKIE[$name]) ;
        return setcookie($name, "", time()-3600) ;
    }
	
    /**
    * Return error
    * 
    * @param int $error_number
    * 
    * @return string
    */
    public function returnError($int = 0) {

	$error = array(
		"Okänt fel",
		"Du har angivit felaktigt användarnamn eller lösenord"
	);
	
	return $this->addNotice($error[$int], "error") ;	
		
    }

    public function setRememberMe() {

        if($this->doSetCookie($this->getUsername()) && $this->doSetCookie($this->getPassword(), "password")) {
            return true ;
        } else {
            return false ;
        }


    }
    public function setUnrememberMe() {
        $this->doUnsetCookie() ;
        $this->doUnsetCookie("password") ;
    }
	
}
